var my_vue = {};
$(function() {
	$("#left").load("../common/gover-left.html");
	var fromSpace = isEmpty(parseQueryString().fromSpace) ? '0' : parseQueryString().fromSpace;
	var type = isEmpty(parseQueryString().type) ? 'dept' : parseQueryString().type;
	var fromGuide= isEmpty(parseQueryString().fromGuide) ? '0' : parseQueryString().fromGuide;
	var code= parseQueryString().code;
	var userType = parseQueryString().userType;
	var currInput = $("#myphone");
	$(".input_text").bind('click', function(e) {
		currInput = $(this);
	});

	$(".query_btns .btn").bind('click', function(e) {
		if($(this).attr("value")) {
			switch($(this).attr("value")) {
				case "clear":
					currInput.val("");
					break;
				case "del":
					currInput.val(currInput.val().substring(0, currInput.val().length - 1));
					break;
				default:
					currInput.val(currInput.val() + $(this).attr("value"));
					break;
			}
		}
	});

	var myValidation = $("#validation-btn").btnValidation({
		time: 60,
		txt: "重新获取验证码(n)"
	});

	my_vue = new Vue({
		el: '#vue_info',
		data: {
			dto: {
				userId: '',
				mobile: '',
				name: '',
				sex: '',
				nation: '',
				birthday: '',
				address: '',
				cardNum: '',
				issued: '',
				life: ''
			},
			welcome: '您好  ',
			oldUser: 0
		},
		methods: {

			changeOp: function() {
				window.location = "scannerIndex.html"
			},
			consult: function() {
				window.location = "readIdentityCard.html"
			},
			exitProcess: function(op) {
				if(op == 'index') {
					$.showConfirm({
						'title': '',
						'content': '是否退出办理？',
						"showClose": false,
						"sureLabel": "取消",
						"cancelLabel": "退出办理",
						'cancel': this.toIndex
					});
				} else if(op == 'scanner') {
					$.showConfirm({
						'title': '',
						'content': '是否退出办理？',
						"showClose": false,
						"sureLabel": "取消",
						"cancelLabel": "退出办理",
						'cancel': this.toScanner
					});
				}
			},
			toIndex: function() {
				window.location = "index.html"
			},
			toScanner: function() {
				window.location = "scannerIndex.html"
			},
			toProject: function() {
				validatePhone();
				validateNcode();
				var myphone = $("#myphone").val();
				var ncode = $("#myinput").val();
				if(validatePhone()==false||validateNcode()==false){
					return false
				}else{					
					this.checkIn(myphone, ncode);
				}
			},

			checkIn: function(myphone, ncode) {
				$.ajax({
					type: "post",
					url: gover.signIn,
					async: true,
					dataType: 'json',
					data: {
						mobile: myphone,
						msgCode: ncode,
						userId: this.dto.userId
					},
					success: function(result) {
						doMask();
						if(result.success) {
							console.log(result.success);
							console.log(result.message);
							my_vue.onlineUser1();
						} else {
							console.log(result.success);
							console.log(result.message);
							unMask();
							showRedMsg("message", "服务器正忙,请稍候重试");
						}

					},
					error: function(result) {
						console.log('保存接口失败:' + toJson(result));
						unMask();
						showRedMsg("message", "服务器正忙,请稍候重试");
					}
				});
			},
			toReturn: function() {
				window.location = "readIdentityCard.html";
			},
			validation: function() {
				myValidation.start();
				var myphone = $("#myphone").val();
				if(myphone != '' && this.oldUser == 0) {
					this.checkMobile(myphone);
				} else {
					this.getMsgCode(myphone);
				}
				setTimeout(function() {
					showRedMsg("message", "验证码过期,请重新获取!");
				}, 60000)
			},
			checkMobile: function(mobile) {
				$.ajax({
					type: "get",
					url: gover.checkMobile,
					async: true,
					dataType: 'json',
					data: {
						mobile: mobile
					},
					success: function(result) {
						if(result.success) {
							console.log(result.data.isUsed);
							if(result.data.isUsed == 0) {
								my_vue.getMsgCode(mobile);
							} else {
								console.log("该手机号已被注册!");
							}
						} else {
							result.message;
						}

					},
					error: function(result) {
						console.log('保存接口失败:' + toJson(result));
						top.$.closePopWin();
					}
				});
			},
			getMsgCode: function(mobile) {
					var t = new Date().getMilliseconds();
				$.ajax({
					type: "get",
					//								url:"http://localhost:8888/oc_tengzhou/gover/getMsgCode.do",
					url: gover.getMsgCode+"?t="+t,
					async: true,
					dataType: 'json',
					data: {
						userId: this.dto.userId,
						mobile: mobile
					},
					success: function(result) {
						if(result.success) {
							console.log(result.data);
							$("#myinput").val(result.data.msgCode);

						} else {
							console.log(result.message);
						}

					},
					error: function(result) {
						console.log('保存接口失败:' + toJson(result));
						top.$.closePopWin();
					}
				});
			},
			exitAttendTo: function() {
				$.openDialog({
					'title': '',
					"showClose": false,
					'width': 824,
					'height': 460,
					'url': 'selfHelpExitChoose.html'
				});
			},
			setWelcome: function() {
				this.welcome += this.dto.name;
				if(this.dto.sex == '男') {
					this.welcome += "先生";
				} else {
					this.welcome += "女士";
				}

				if(isNotEmpty(this.dto.cardNum)) {
					this.welcome += '(';
					this.welcome += this.dto.cardNum.substring(0, 5);
					this.welcome += '******';
					this.welcome += this.dto.cardNum.substring(13, 18);
					this.welcome += ')';
				}
				if(isNotEmpty(this.dto.mobile)) {
					this.welcome += "  请获取验证码登陆";
				} else {
					this.welcome += "  请输入手机号登入";
				}
			},
			onlineUser1: function() {
				this.$http.post(gover.onlineUser, {
					userId: this.dto.userId,
					mobile: this.dto.mobile,
					name: this.dto.name,
					sex: this.dto.sex,
					nation: this.dto.nation,
					birthday: this.dto.birthday,
					address: this.dto.address,
					cardNum: this.dto.cardNum,
					issued: this.dto.issued,
					life: this.dto.life
				}, {
					emulateJSON: true
				}).then(function(result) {
					unMask();
					if(result.data.success){
						sessionStorage.setItem("userId", this.dto.userId);
						if(fromSpace=='1'){
							window.location = "../space/home.html";
						} else if(fromGuide=='1'){
							window.location = "chooseServiceType.html?userType="+userType
							+"&code="+code;
						}else{
							window.location = "selfHelpBusinessChoose.html?type="+type;
						}
					} else{
						unMask();
						showRedMsg("message", "服务器正忙,请稍候重试");
					}
				}, function(result) {
					unMask();
					showRedMsg("message", "服务器正忙,请稍候重试");
				})
			}

		},
		ready: function() { //初始化获得总页面数和列表数据
			var dto = console.log(sessionStorage.getItem("dto"));
			this.dto = toObj(sessionStorage.getItem("dto"));
			if(isNotEmpty(this.dto.mobile)) {
				this.oldUser = 1;
				$("#myphone").attr("readOnly","true");
			}
			this.setWelcome();
		}
	});
});